exports.buy = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.num) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var num = parseInt(req.args.num);
        if (num < 1 || num > 999) {
            resp.code = 1; resp.desc = 'invalid num'; break;
        }

        var user = player.user;
        if( !user.league_war ) {
            resp.code = 1; resp.desc = 'no league_war'; break;
        }

        var gloryShopConf = gConfGloryShop[req.args.id];
        if( !gloryShopConf ) {
            resp.code = 1; resp.desc = 'invalid id'; break;
        }

        if( user.league_war.grade_level < gloryShopConf.Grade ) {
            resp.code = 1; resp.desc = 'grade too low'; break;
        }

        var gloryCost = gloryShopConf.Price * num;
        if( user.league_war.glory < gloryCost ) {
            resp.code = 1; resp.desc = 'not enough glory'; break;
        }

        var awards = [['material', gloryShopConf.Material, num]];
        awards = player.addAwards(awards);

        user.league_war.glory -= gloryCost;

        player.markDirty('league_war.glory');

        resp.data.glory = -gloryCost;
        resp.data.awards = awards;
    }while(false);

    onHandled();
}

exports.buy_move = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var leagueWar = user.league_war;
        var costCash = gConfGlobal.LeagueWarBuyMoveCash;
        if( !player.costResource('cash', costCash) ) {
            resp.code = 1; resp.desc = 'not enough cash'; break;
        }

        leagueWar.move += 1;
        player.markDirty('league_war.move');

        resp.data.cash = -costCash;

    }while(false);

    onHandled();
}

exports.buy_energy = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var leagueWar = user.league_war;
        var buyConf = gConfBuy[leagueWar.buy_energy + 1];
        if( !buyConf || !buyConf.CashLeagueWarEnergy ) {
            resp.code = 1; resp.desc = 'no times'; break;
        }
        
        var costCash = buyConf.CashLeagueWarEnergy;
        if( !player.costResource('cash', costCash, leagueWar.buy_energy) ) {
            resp.code = 1; resp.desc = 'not enough cash'; break;
        }

        leagueWar.buy_energy += 1;
        leagueWar.energy += gConfGlobal.LeagueWarEnergyPerCost; 

        player.markDirty('league_war.buy_energy');
        player.markDirty('league_war.energy');

        resp.data.cash = -costCash;
    }while(false);

    onHandled();
}

exports.get_ex_energy = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }
    
    req.args.ex_energy_time = user.league_war.ex_energy_time;
    requestWorld(req, resp, function(){
        if( resp.code == 0 ) {
            var now = common.getTime();

            var leagueWar = user.league_war;
            var addEnergy = gConfGlobal.LeagueWarGotExEnergy;
            leagueWar.energy += addEnergy;
            leagueWar.ex_energy_time = now;
    
            player.markDirty('league_war.energy');
            player.markDirty('league_war.ex_energy_time');

            resp.data.energy = addEnergy;
            resp.data.ex_energy_time = now;
        }
    
        onHandled();
    });
}

exports.get = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }
    
    var league_war = user.league_war;
    req.args.league_name = user.league.name; 
    req.args.grade_level =  league_war.grade_level;

    requestWorld(req, resp, function(){
        if( 0 == resp.code ) {
            var now = common.getTime();
            if( resp.data.enter ) {
                league_war.energy = 0;
                league_war.energy_time = now; 
                league_war.ex_energy_time = 0;
                league_war.move_time = now;
                league_war.move = 1;

                player.markDirty('league_war.energy');
                player.markDirty('league_war.energy_time');
                player.markDirty('league_war.ex_energy_time');
                player.markDirty('league_war.move_time');
                player.markDirty('league_war.move');
            }
            
            // 更新迁徙次数
            var moveOutputPeriod = gConfGlobal.LeagueWarMoveOutputHours * 3600;
            var addMove = parseInt((now - league_war.move_time) / moveOutputPeriod); 
            if( addMove ) {
                league_war.move += addMove; 
                league_war.move_time += addMove * moveOutputPeriod; 

                player.markDirty('league_war.move');
                player.markDirty('league_war.move_time');
            }

            resp.data.move = league_war.move;
            resp.data.ex_energy_time = league_war.ex_energy_time;
            resp.data.last_fight_time = league_war.last_fight_time;
        }

        onHandled();
    });
}

exports.get_city = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }

    requestWorld(req, resp, function(){
        if( resp.code == 0 ) {
            player.updateLeagueWarEnergy();
            resp.data.energy = player.user.league_war.energy;
        }
    
        onHandled();
    });
}

exports.get_energy = function(player, req, resp, onHandled) {
    var user = player.user;
    var league_war = user.league_war;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        player.updateLeagueWarEnergy();
        if( league_war.energy <= 0 ) {
            resp.code = 1; resp.desc = 'not enough energy'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }

    req.args.energy = league_war.energy;
    req.args.grade_level = league_war.grade_level;

    requestWorld(req, resp, function(){
        if( 0 == resp.code ) {
            league_war.energy -= resp.data.energy_got;
            player.markDirty('league_war.energy');

            resp.data.energy = league_war.energy;
        }

        onHandled();
    });
}

exports.fight = function(player, req, resp, onHandled) {
    var user = player.user;
    var now = common.getTime();
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var lastFightTime = user.league_war.last_fight_time;
        if( (now - lastFightTime) < gConfGlobal.LeagueWarFightColdTime - 1 ) {
            resp.code = 1; resp.desc = 'in cold time'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }

    req.args.grade_level = user.league_war.grade_level;
    requestWorld(req, resp, function(){
        if( 0 == resp.code ) {
            user.league_war.last_fight_time = now;
            player.markDirty('league_war.last_fight_time'); 

            if( resp.data.grade_xp ) {
                player.addGradeXp(resp.data.grade_xp);

                var league_war = player.user.league_war;
                resp.data.grade_xp = league_war.grade_xp;
                resp.data.grade_level = league_war.grade_level;
            }
        }
        onHandled();
    });
}

exports.move = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        if( user.league_war.move < 1 ) {
            resp.code = 1; resp.desc = 'not enough move'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }

    req.args.league = user.league.name; 
    requestWorld(req, resp, function(){
        if( 0 == resp.code ) {
            user.league_war.move -= 1;
            player.markDirty('league_war.move');
        }

        onHandled();
    });
}

exports.rank_global = function(player, req, resp, onHandled) {
    requestWorld(req, resp, onHandled);
}

exports.rank_personal = function(player, req, resp, onHandled) {
    requestWorld(req, resp, onHandled);
}

exports.end_war = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }

    requestWorld(req, resp, function(){
        if( resp.code == 0 && resp.data.league_king ) {
            if( !user.activity.hasOwnProperty('league_king') ) {
                user.activity.league_king = {};
            }
            
            var rewardLeagueKing = resp.data.league_king;
    
            if( !(rewardLeagueKing in user.activity.league_king) ) {
                user.activity.league_king[rewardLeagueKing] = 0;
                user.activity.league_king.leader_group = resp.data.leader_group ? rewardLeagueKing : 0;

                player.markDirty('activity.league_king');
            }
        }
        onHandled();
    });
}

exports.get_reward = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }

    req.args.grade_level = user.league_war.grade_level;
    requestWorld(req, resp, function(){
        if( 0 == resp.code ) {
            var leagueWar = user.league_war;
            if( resp.data.grade_xp ) {
                player.addGradeXp(resp.data.grade_xp);

                resp.data.grade_xp = leagueWar.grade_xp;
                resp.data.grade_level = leagueWar.grade_level;
            }

            if( resp.data.glory > 0 ) {
                var maxGlory = gConfGradeLevel[leagueWar.grade_level].GloryLimit;
                var gloryGot = Math.min(maxGlory-leagueWar.glory, resp.data.glory);
                resp.data.glory = gloryGot;

                leagueWar.glory += gloryGot;
                player.markDirty('league_war.glory');
            }

            var awards = [];
            if( resp.data.topone ) {
                awards.push(['material', 47, 1]);
            }

            awards = player.addAwards(awards);
            resp.data.awards = awards;
        }
        onHandled();
    });
}

exports.preview_war = function(player, req, resp, onHandled) {
    requestWorld(req, resp, onHandled);
}

exports.register = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }

    req.args.league_name = user.league.name;
    requestWorld(req, resp, onHandled);
}

exports.get_register = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( !user.league.name ) {
            resp.code = 1; resp.desc = 'not in league'; break;
        }

        if( user.status.level < gConfGlobal.LeagueWarUserLevelMin ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

    }while(false);

    if( resp.code != 0 ) {
        onHandled();
        return;
    }

    req.args.league_name = user.league.name;
    requestWorld(req, resp, onHandled);
}
